package com.zhuoshi.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import com.zhuoshi.entity.Client;
import com.zhuoshi.entity.Complex;
import com.zhuoshi.entity.Indent;
import com.zhuoshi.entity.PayMentTypeCustom;
import com.zhuoshi.entity.Paymenttype;
import com.zhuoshi.entity.Project;
import com.zhuoshi.entity.Receive;


import com.zhuoshi.entity.RoomCustom;
import com.zhuoshi.entity.Unit;

import com.zhuoshi.service.ClientService;
import com.zhuoshi.service.ComplexService;
import com.zhuoshi.service.IndentService;
import com.zhuoshi.service.MarketingService;
import com.zhuoshi.service.PayMentTypeService;
import com.zhuoshi.service.ProjectService;
import com.zhuoshi.service.ReceiveService;
import com.zhuoshi.service.RoomService;
import com.zhuoshi.service.UnitService;

/*
 * 
 * Author 海生
 */
//销售机会
@Controller
@RequestMapping(value="xiaoshoujihui")
public class ReceiveController {
	
	@Autowired
	private ReceiveService receiveService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private ComplexService complexService;
	@Autowired
	private UnitService unitService;
	@Autowired
	private RoomService roomService;
	@Autowired
	private ClientService clientService;
	@Autowired
	private MarketingService marketingService;
	@Autowired
	private PayMentTypeService payMentTypeService;
	
	
	//查询所有销售机会
	@RequestMapping("xiaoShouJiHuiList.action")
	public ModelAndView selectAlljiedaijilu() throws Exception{
		List<Receive> receiveList = receiveService.selectAlljiedaijilu();
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("receiveList", receiveList);
		modelAndView.setViewName("/Admin/XiaoShouJiHui.jsp");
		return modelAndView;
	}
	
	//查询项目Project，楼栋Complex 单元Unit 房间Room 等信息显示
/*	@RequestMapping("AddXiaoShouJiHui.action")
	public ModelAndView selectProjectComplexUnitRoom() throws Exception{
		List<ReveiceCustom> reveiceCustomList = receiveService.selectProjectComplexUnitRoom();
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("reveiceCustomList", reveiceCustomList);
		modelAndView.setViewName("/Admin/AddXiaoShouJiHui.jsp");
		return modelAndView;
	}*/
	
	//查询所有的工程
	@RequestMapping("AddXiaoShouJiHui.action")
	public ModelAndView selectAllProject() throws Exception{
		List<Project> projectList =projectService.selectAllProjectLiu();
		List<Client> clientList = clientService.selectClientAllLu();
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("projectList", projectList);	
		modelAndView.addObject("clientList", clientList);
		modelAndView.setViewName("/Admin/AddXiaoShouJiHui.jsp");
		return modelAndView;
	}
	//查询所有楼栋
	@RequestMapping("selectAllComplex.action")
	public ModelAndView selectAllComplex(HttpServletResponse response,int projectid) throws Exception{
		List<Complex> complexList = complexService.selectComplexByProjectIdLiu(projectid);
		JSONObject json=new JSONObject();
		json.put("complexList", complexList);
		PrintWriter out=null;
		try {
			out=response.getWriter();
			out.print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	//查询所有单元
	@RequestMapping("selectAllUnit.action")
	public ModelAndView selectAllUnit(HttpServletResponse response,int complexid) throws Exception{
		List<Unit> unitList = unitService.selectUnitByComplexIdLiu(complexid);
		JSONObject json=new JSONObject();
		json.put("unitList", unitList);
		PrintWriter out=null;
		try {
			out=response.getWriter();
			out.print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	//查询所有房间
	@RequestMapping("selectAllroom.action")
	public ModelAndView selectAllroom(HttpServletResponse response,@RequestParam("unitId")Integer unitId) throws Exception{
		List<RoomCustom> roomList = roomService.selectRoomByIdsLu(unitId);
		JSONObject json=new JSONObject();
		json.put("roomList", roomList);
		PrintWriter out=null;
		try {
			out=response.getWriter();
			out.print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	//selectClientIdAndName.action  根据电话查询客户ID和名称
	@RequestMapping("selectClientIdAndName.action")
	public ModelAndView selectClientIdAndNameByPhone(HttpServletRequest request,HttpServletResponse response,@RequestParam("phone")String phone) throws Exception{
		request.getCharacterEncoding();
		Client clientList = clientService.selectClientIdAndNameByPhone(phone);		
		System.out.println("...."+clientList);
		ModelAndView modelAndView = new ModelAndView();
		Integer clientid = clientList.getId();
		String clientname = clientList.getClientidname();
		modelAndView.addObject("clientid", clientid);
		modelAndView.addObject("clientname", clientname);
		modelAndView.setViewName("/Admin/AddXiaoShouJiHui.jsp");
		return modelAndView;
		
//		System.out.println("client++++"+clientList);
//		JSONObject json=new JSONObject();
//		json.put("clientList", clientList);
//		PrintWriter out=null;
//		try {
//			out=response.getWriter();
//			out.print(json);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		return null;
	}

	//确认添加销售机会 QuerenAddXiaoShouJiHui.action
	@RequestMapping("QuerenAddXiaoShouJiHui.action")
	public String QuerenAddXiaoShouJiHui(Receive receive) throws Exception{
		int rows = receiveService.QuerenAddXiaoShouJiHui(receive);	
		
		if(rows>0){
			return "redirect:xiaoShouJiHuiList.action";
		}else{
			return "/error.jsp";
		}
	}
	
	//删除销售机会 deleteXiaoShouJiHui.action
	@RequestMapping("deleteXiaoShouJiHui.action")
	public String deleteXiaoShouJiHui(int id) throws Exception{
		int rows = receiveService.deleteXiaoShouJiHui(id);
		if(rows>0){
			return "redirect:xiaoShouJiHuiList.action";
		}else{
			return "/error.jsp";
		}
	}
	
	//根据id查询
	@RequestMapping("adddingdan.action")
	public ModelAndView selectById(int id) throws Exception{
		Receive receive = receiveService.selectById(id);
		List<PayMentTypeCustom> payType = payMentTypeService.selectPayMentTypeList();
		ModelAndView model = new ModelAndView();
		model.addObject("receive", receive);
		model.addObject("payType", payType);
		model.setViewName("/Admin/addDingdan.jsp");
		return model;
	}
	
	
}
